/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package work.hutool.util;


import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


/**
 * @author Sakura Shen
 * @description XmlUtil + w3c.dom使用
 *
 * 使用XmlUtil.parseXml() 解析xml字符串或者文件
 * @date 2021/8/26
 */
public class XmlUtilTest {
    public static void main(String[] args) {

        String xmlStr  = "<?xml version = \"1.0\" encoding = \"utf-8\"?>\n" +
                "<School>\n" +
                "    <Student>\n" +
                "        <Name>沈浪</Name>\n" +
                "        <Num>1006010022</Num>\n" +
                "        <Classes>信管2</Classes>\n" +
                "        <Address>浙江杭州3</Address>\n" +
                "        <Tel>123456</Tel>\n" +
                "    </Student>\n" +
                "    <Student>\n" +
                "        <Name>沈1</Name>\n" +
                "        <Num>1006010033</Num>\n" +
                "        <Classes>信管1</Classes>\n" +
                "        <Address>浙江杭州4</Address>\n" +
                "        <Tel>234567</Tel>\n" +
                "    </Student>\n" +
                "    <Student>\n" +
                "        <Name>沈2</Name>\n" +
                "        <Num>1006010044</Num>\n" +
                "        <Classes>生工2</Classes>\n" +
                "        <Address>浙江杭州1</Address>\n" +
                "        <Tel>345678</Tel>\n" +
                "    </Student>\n" +
                "    <Student>\n" +
                "        <Name>沈3</Name>\n" +
                "        <Num>1006010055</Num>\n" +
                "        <Classes>电子2</Classes>\n" +
                "        <Address>浙江杭州2</Address>\n" +
                "        <Tel>456789</Tel>\n" +
                "    </Student>\n" +
                "</School>";

        // XmlUtil把字符串转化为Document对象
        Document doc =  XmlUtil.parseXml(xmlStr);
        System.out.println("处理该文档的DomImplementation对象  = "+ doc.getImplementation());
        // 得到文档名称为Student的元素的节点列表
        // getElementsByTagName 这个方法很厉害
        NodeList nList = doc.getElementsByTagName("Student");
        //遍历该集合，显示结合中的元素及其子元素的名字
        for(int i = 0; i< nList.getLength() ; i ++){
            Element node = (Element)nList.item(i);
            System.out.println("Name: "+ node.getElementsByTagName("Name").item(0).getFirstChild().getNodeValue());
            System.out.println("Num: "+ node.getElementsByTagName("Num").item(0).getFirstChild().getNodeValue());
            System.out.println("Classes: "+ node.getElementsByTagName("Classes").item(0).getFirstChild().getNodeValue());
            System.out.println("Address: "+ node.getElementsByTagName("Address").item(0).getFirstChild().getNodeValue());
            System.out.println("Tel: "+ node.getElementsByTagName("Tel").item(0).getFirstChild().getNodeValue());
        }

        System.out.println("AAA");

    }
}
